home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / TinyMeter / Source / TinyMeter_main / volumes.c < prev   
C/C++ Source or Header  |  1997-02-27  |  5KB  |  182 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dos.h>
  4. #include <dos/dosextens.h>
  5. #include <dos/dostags.h>
  6. #include <dos/notify.h>
  7. #include <string.h>
  8. #include <libraries/WBStart.h>
  9. #include <workbench/startup.h>
  10. #include <workbench/workbench.h>
  11. #include <intuition/classes.h>
  12. #include <clib/macros.h>
  13. #include "gaugeclass.h"
  14. #include "tinymeter.h"
  15. #include "launchclass.h"
  16.  
  17. BOOL CopyPathList(struct PathList **pla, struct PathList **plc,struct PathList *oldpl)
  18. {
  19.     struct PathList *pl1=oldpl,*pl2=*plc,*pl3=NULL;
  20.     while (pl1)
  21.     {
  22.     if (!(pl3 || (pl3=(struct PathList *)AllocVec(sizeof(struct PathList),MEMF_CLEAR|MEMF_PUBLIC)))) return(FALSE);
  23.     if (pl3->PathLock=DupLock(pl1->PathLock))
  24.     {
  25.         if (*pla) pl2->NextPath=MKBADDR(pl3);
  26.         else *pla=pl3;
  27.         pl2=pl3;
  28.         pl3=NULL;
  29.     }
  30.     pl1=BADDR(pl1->NextPath);
  31.     }
  32.     FreeVec(pl3);
  33.     *plc=pl2;
  34.     return(TRUE);
  35. }
  36.  
  37. void FreePathList(struct PathList *pla)
  38. {
  39.     if (pla)
  40.     {
  41.     struct PathList *pl2;
  42.     do
  43.     {
  44.         pl2=BADDR(pla->NextPath);
  45.         UnLock(pla->PathLock);
  46.         FreeVec(pla);
  47.     }
  48.     while (pla=pl2);
  49.     }
  50. }
  51.  
  52. BOOL InitWorkbenchPath(struct tm_data *data)
  53. {
  54.     struct Process *wbproc=(struct Process *)FindTask("Workbench");
  55.     if(!wbproc)     wbproc=(struct Process *)FindTask("DirectoryOpus");
  56.  
  57.     if (wbproc && (wbproc->pr_Task.tc_Node.ln_Type==NT_PROCESS))
  58.     {
  59.     struct CommandLineInterface *wbcli=BADDR(wbproc->pr_CLI);
  60.     if (wbcli)
  61.     {
  62.         struct PathList *dummy=0L; data->GlobalPath=0L;
  63.         if (CopyPathList(&data->GlobalPath,&dummy,(struct PathList *) BADDR(wbcli->cli_CommandDir)));
  64.     }
  65.     }
  66.     return(TRUE); 
  67. }
  68.  
  69. void FreeWorkbenchPath(struct tm_data *data)
  70. {
  71.     FreePathList(data->GlobalPath); data->GlobalPath=0L;
  72. }
  73.  
  74. void RunCLI(struct lau_entry *entry, struct tm_data *data, struct WBArg *wbarg, ULONG numarg)
  75. {
  76.     BOOL            rc=FALSE;
  77.     char           *def;
  78.     char           *argstr=0L;
  79.     BPTR            newcd,
  80.             ofh,
  81.             oldcd,
  82.             ifh;
  83.     struct MsgPort *newct=NULL;
  84.  
  85.     if(wbarg)
  86.     {
  87.     if(argstr=(char *)pAllocVec(1024L))
  88.     {
  89.         char    *actpos;
  90.         int     i;
  91.         strcpy(argstr,entry->lau_cmd);
  92.         strcat(argstr," ");
  93.         actpos=&argstr[strlen(argstr)];
  94.         for(i=0;i<numarg;i++)
  95.         {
  96.         NameFromLock(((struct WBArg *)&wbarg[i])->wa_Lock,actpos,512L);
  97.         AddPart(actpos,((struct WBArg *)&wbarg[i])->wa_Name,512L);
  98.         strcat(actpos," ");
  99.         actpos=&argstr[strlen(argstr)];
  100.         }
  101.     }
  102.     }
  103.     if(entry->lau_pth)  newcd=Lock(entry->lau_pth,SHARED_LOCK);
  104.     else                newcd=0L;
  105.     if(!newcd)
  106.     {
  107.     char *foo;
  108.     int i;
  109.     foo=(char *)FilePart(entry->lau_cmd);
  110.     if(def=(char *)pAllocVec(foo-entry->lau_cmd+2L))
  111.     {
  112.         for(i=0;i<(foo-entry->lau_cmd);i++) def[i]=entry->lau_cmd[i]; def[i]=0;
  113.         newcd=Lock(def,SHARED_LOCK);
  114.         pFreeVec((ULONG *)def);
  115.     }
  116.     }
  117.     if(!(ofh=Open(entry->lau_out, MODE_NEWFILE)))
  118.      ofh=Open("NIL:",         MODE_NEWFILE);
  119.     if(ofh)
  120.     {
  121.     if (IsInteractive(ofh))
  122.     {
  123.         struct MsgPort *oldct;
  124.         newct=((struct FileHandle *) BADDR(ofh))->fh_Type;
  125.         oldct=(struct MsgPort *)SetConsoleTask(newct);
  126.         ifh=Open("CONSOLE:",MODE_OLDFILE);
  127.         SetConsoleTask(oldct);
  128.     }
  129.     else ifh=Open("NIL:",MODE_OLDFILE);
  130.     if (ifh)
  131.     {
  132.         struct PathList *pla=NULL,*plc=NULL;
  133.         oldcd=CurrentDir(newcd);
  134.         if(CopyPathList(&pla,&plc,data->GlobalPath))
  135.         {
  136.         if (SystemTags(argstr ? argstr : entry->lau_cmd,
  137.             SYS_Output,     ofh,
  138.             SYS_Input,      ifh,
  139.             SYS_Asynch,     TRUE, 
  140.             SYS_UserShell,  TRUE,
  141.             NP_StackSize,   entry->lau_stack < 4096L ? 4096L : entry->lau_stack,
  142.             NP_Priority,    0,
  143.             NP_Path,        MKBADDR(pla),
  144.             NP_ConsoleTask, newct,
  145.             TAG_DONE)!=-1) rc=TRUE;
  146.         CurrentDir(oldcd);
  147.         if(!rc)
  148.         {
  149.             Close(ifh);
  150.             FreePathList(pla);
  151.         }
  152.         }
  153.     }
  154.     if(!rc)
  155.     {
  156.         Close(ofh);
  157.         DisplayBeep(0);
  158.     }
  159.     }
  160.     UnLock(newcd);
  161.     pFreeVec((ULONG *)argstr);
  162. }
  163.  
  164. void RunWB(struct lau_entry *entry, struct tm_data *data, struct WBArg *wbarg, ULONG numarg)
  165. {
  166.     if(WBStartTags(WBStart_Name,                            entry->lau_cmd,
  167.            WBStart_DirectoryName,                   entry->lau_pth,
  168.            WBStart_Stack,                           entry->lau_stack < 4096L ? 4096L : entry->lau_stack,
  169.            wbarg ? WBStart_ArgumentList : TAG_END,  wbarg,
  170.            WBStart_ArgumentCount,                   numarg,
  171.            TAG_END))
  172.     {
  173.     DisplayBeep(0);
  174.     }
  175. }
  176.  
  177. void RunFile(struct lau_entry *entry, struct tm_data *data, struct WBArg *wbarg, ULONG numarg)
  178. {
  179.     if(entry->lau_type==0) RunWB(entry,data,wbarg,numarg);
  180.     else                   RunCLI(entry,data,wbarg,numarg);
  181. }
  182.